package com.example.cs_jump.Util;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.os.Environment;
import android.util.Log;

import com.example.cs_jump.Bean.JsonBean;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;

import org.jetbrains.annotations.NotNull;
import org.jsoup.*;
import org.jsoup.nodes.Document;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

import okhttp3.Call;
import okhttp3.Callback;
import okhttp3.Headers;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;
import okio.ByteString;

public class HttpConnectionFilter {

    public static String[] mapUrl = {
            "http://81.69.8.22:8080/map/nuke.json",
            "http://81.69.8.22:8080/map/dust.json",
            "http://81.69.8.22:8080/map/cache.json",
            "http://81.69.8.22:8080/map/inferno.json",
            "http://81.69.8.22:8080/map/mirage.json",
            "http://81.69.8.22:8080/map/overpass.json",
            "http://81.69.8.22:8080/map/train.json",
            "http://81.69.8.22:8080/map/vertigo.json"
    };
    static String result = "";
    static int flag = 0;

    public static String getMapJsonFilter(int i) throws IOException {
//        Connection.Response document = Jsoup.connect(mapUrl[i]).timeout(6000).userAgent("Mozilla/5.0" +
//                "Windows;U;Windows NT 5.1;zh-CN; rv:1.9.2.15").ignoreContentType(true).execute();
//        Document document=Jsoup.connect(mapUrl[i]).get();
//        String result = document.body();
//        String respone =Jsoup.connect(mapUrl[i]).ignoreContentType(true)
//                .method(Connection.Method.GET).header("Content-Type","application/json")
//                .requestBody()

//        Document document = Jsoup.connect(mapUrl[i]).timeout(5000).userAgent("Mozilla/5.0" + "Windows;U;Windows NT 5.1;zh-CN; " +
//                "rv:1.9.2.15").ignoreContentType(true)
//                .header("Content-Type", "application/json")
//                .get();
//        result = document.html();
//        return result;

        OkHttpClient client = new OkHttpClient();
        Request request = new Request.Builder()
                .get()
                .url(mapUrl[i])
                .build();
        Call call = client.newCall(request);
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                result = response.body().string();
                // Log.d("json", result);
            }
        });
        return result;

    }

    public static int downFile(String url, Context context, int count) {
        OkHttpClient client = new OkHttpClient.Builder()
                .connectTimeout(10000, TimeUnit.MILLISECONDS)
                .build();
        Request request = new Request.Builder()
                .get()
                .url(url)
                .build();
        Call call = client.newCall(request);
        new Thread(new Runnable() {
            @Override
            public void run() {
                FileOutputStream fos = null;
                InputStream inputStream = null;
                try {
                    Response excute = call.execute();
                    //Headers headers = excute.headers();
//                    for (int i = 0; i < headers.size(); i++) {
//                        String key = headers.name(i);
//                        String value = headers.value(i);
//                        Log.d("downFile", key + "==========" + value);
//                    }
                    String filename = count + ".json";
                    File outFile = new File(context.getApplicationContext().getExternalFilesDir(Environment.DIRECTORY_DOCUMENTS) + File.separator + filename);


                    if (!outFile.getParentFile().exists()) {
                        outFile.mkdir();
                    }
                    if (!outFile.exists()) {
                        outFile.createNewFile();
                    }

                    fos = new FileOutputStream(outFile);

                    if (excute.body() != null) {

                        Sqlite sqlite=new Sqlite(context.getApplicationContext());
                        SQLiteDatabase db=sqlite.getWritableDatabase();

                        inputStream = excute.body().byteStream();
                        byte[] buffer = new byte[2048];
                        int len;
                        while ((len = inputStream.read(buffer, 0, buffer.length)) != -1) {
                            fos.write(buffer, 0, len);
                        }
                        fos.flush();
                        flag = 1;
                    }

                } catch (IOException e) {
                    e.printStackTrace();
                } finally {
                    if (fos != null) {
                        try {
                            fos.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                    if (inputStream != null) {
                        try {
                            inputStream.close();
                        } catch (IOException e) {
                            e.printStackTrace();
                        }
                    }
                }

            }
        }).start();
        return flag;
    }

    public void createDataBase(Context context){


    }
}
